package com.dpiinc.TechPage;

import android.os.Build;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import android.view.View;
import android.view.WindowManager;

import com.dpiinc.TechPage.user.utils.StatusBarUtil;
import com.gyf.immersionbar.ImmersionBar;

/**BaseActivity
 * Created by hu489_000 on 2018-06-24.
 */

public class BaseActivity extends AppCompatActivity {

    private ImmersionBar mImmersionBar;
    private Toolbar toolbar;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mImmersionBar = ImmersionBar.with(this);
        mImmersionBar.fitsSystemWindows(true)
                .statusBarColor(R.color.white)
                .statusBarDarkFont(true, 0.2f);
        mImmersionBar.init();

        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        if (toolbar != null) {
            //设置toolbar
            setSupportActionBar(toolbar);
            getSupportActionBar().setTitle("");
            toolbar.setNavigationIcon(R.drawable.toolbar_back);
            toolbar.setNavigationOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    onBackPressed();
                }
            });
        }

        setStatusBarWhiteAndTextBlack();
    }

    public void setTitle(String title) {
        if (toolbar != null) {
            toolbar.setTitle(title);
        }
    }

    public void setBackWhite() {
        if (toolbar != null) {
            toolbar.setNavigationIcon(R.drawable.toolbar_back);
            toolbar.setTitleTextColor(getResources().getColor(R.color.color_151515));
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
//        closeInput();
        //TODO Yang
//        if (mImmersionBar != null)
//            mImmersionBar.destroy();  //必须调用该方法，防止内存泄漏，不调用该方法，如果界面bar发生改变，在不关闭app的情况下，退出此界面再进入将记忆最后一次bar改变的状态
    }
//
    public void setstatusBarColor(int resId) {
//        mImmersionBar.statusBarColor(R.color.white)
//                .statusBarDarkFont(true, 0.2f);
//        mImmersionBar.init();
    }

    public void setStatusBarWhiteAndTextBlack() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
            StatusBarUtil.setStatusBarColor(this,R.color.white);
            StatusBarUtil.StatusBarLightMode(this); //设置白底黑字
//            mImmersionBar.statusBarColor(R.color.white).statusBarDarkFont(true, 0.2f);
//            mImmersionBar.init();
        }
    }

//    private void closeInput() {
//        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
//        if (imm.isActive()) {
//            InputMethodManager inputMethodManager =(InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
//            inputMethodManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
//        }
//    }
}
